package com.hh.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.hh.service.MsmService;
import com.hh.utils.RandomUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Service
public class MsmServiceImpl implements MsmService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public Boolean sendMsg(String phone) {
        FileOutputStream file = null;
        String code = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)) {
            return true;
        } else {
            code = RandomUtil.getSixBitRandom();
            // 存入redis并设置5分钟的过期时间
            redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);

            try {
/*                DefaultProfile profile =
                        DefaultProfile.getProfile("default", "LTAI5tSTKECgQvx1Ui4Y85MR", "g3B9XKHIZWSHC92xoLDKJgoYR2AA3F");
                IAcsClient client = new DefaultAcsClient(profile);

                //设置相关固定的参数
                CommonRequest request = new CommonRequest();
                //request.setProtocol(ProtocolType.HTTPS);
                request.setMethod(MethodType.POST);
                request.setDomain("dysmsapi.aliyuncs.com");
                request.setVersion("2017-05-25");
                request.setAction("SendSms");

                ///设置发送相关的参数
                HashMap<String, String> codeParam = new HashMap<>();
                codeParam.put("code", code);

                // 签名(SignName)
                request.putQueryParameter("SignName","ManEdu在线教育");
                //模板Code(TemplateCode)
                request.putQueryParameter("TemplateCode", "SMS_229647314");

                //手机号(PhoneNumbers)
                request.putQueryParameter("PhoneNumbers", phone);
                //验证码(TemplateParam.json格式)
                request.putQueryParameter("TemplateParam", JSON.toJSONString(codeParam));

                HttpResponse httpResponse = client.getCommonResponse(request).getHttpResponse();*/

                try {
                    file = new FileOutputStream("D:\\code.txt",true);
                    String content = phone + "：\t[ManEdu在线教育]尊敬的用户，您的验证码为：" + code + "，5分钟内有效，如非本人操作，请勿泄露此验证码给他人！[" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "]\n";
                    byte[] bytes = content.getBytes();
                    file.write(bytes);
                } catch (IOException e1) {
                    e1.printStackTrace();
                } finally {
                    file.close();
                }
//                return httpResponse.isSuccess();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    }
}
